<?php

namespace app\api\controller;

use app\data\Data;
use app\api\model\Preg;
use app\api\model\News as news_list;
use think\Request;

class Complain extends Index
{
    /**
     * 交易申诉
     * 显示资源列表
     * 首页，页面显示
     * @return \think\Response
     */
    public function complain_index()
    {
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $username = (isset($_GET['username']) && !empty($_GET['username']) ? $_GET['username'] : null);
        $logmin = (isset($_GET['logmin']) && $_GET['logmin']) ? strtotime($_GET['logmin']) : 0;
        $logmax = (isset($_GET['logmax']) && $_GET['logmax']) ? (strtotime($_GET['logmax']) + 24 * 60 * 60 - 1) : time();
        $where['a.status'] = array('in', array(1, 2));
        $where['a.static'] = (isset($_GET['static']) && $_GET['static']) ? $_GET['static'] : array('neq', 0);
        $where['u.username|a.nick|a.phone'] = array('like', "%$username%");
        $where['a.time'] = array(array('egt', $logmin), array('elt', $logmax), 'and');

        $join[0] = "__USER__ u";
        $join[1] = "u.uid = a.uid";
        $field = "a.id,a.order,a.image,a.problem,a.pic,a.nick,a.phone,a.time,a.result,a.static,u.username";

        $data= db('complain')->alias('a')->join($join[0], $join[1])->order('time desc')->where($where)->field($field)->limit(10)->page($page)->select();
        //屏蔽关键字
		$match = new Preg();
		foreach($data as $key=>$val){
                $html=$match->replace($val['problem']);
				$data[$key]['problem']=$html;
            }
			//
			$res['data'] =$data;
		$res['count'] = db('complain')->alias('a')->join($join[0], $join[1])->where($where)->count();
		if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '交易申诉信息获取成功', $res);
        } else {
            $this->_jsonp('1000', '交易申诉信息获取失败', null);
        }
    }

	/**
	public function download(){
		$where['id'] = $_GET['id'];
		$res = db('complain')->where($where)->value('pic');
		$pic = json_decode($res,true);
		for($i=0; $i < count($pic); $i++){
			$king[$i] = "/Images/appeal/$pic[$i]";
			header("Content-Type: application/octet-stream");
			header("Content-Disposition: attachment;filename=".$king[$i]);
			ob_clean();
			flush(); 
			readfile("$king[$i]");
		}
	}*/

	
    /**
     * 交易申诉
     * 未受理转
     * 待受理
     */
    public function complain_dispose()
    {
        $where['id'] = trim(input('get.id'));
        $data['static'] = 2;
        if (isset($where) || !empty($where)) {
            $zgh_complain = new Data('complain');
            $zgh_complain->_update($data, $where);
            action_log('交易申诉未受理转待受理', 'log_admin', 1);
            $this->_jsonp('200', '操作成功', null);
        } else {
            action_log('交易申诉未受理转待受理', 'log_admin');
            $this->_jsonp('1000', '操作失败', null);
        }
    }

    /**
     * 交易申诉
     * 待受理
     * 转已受理
     */
    public function complain_replay()
    {
        $where['id'] = trim(input('get.id'));
        $data['static'] = 3;
        $data['result'] = (isset($_GET['result']) && $_GET['result']) ? trim($_GET['result']) : '';
        if (isset($where) || !empty($where)) {
            $zgh_complain = new Data('complain');
            $zgh_complain->_update($data, $where);
            action_log('交易申诉待受理已受理', 'log_admin', 1);
            $this->_jsonp('200', '操作成功', null);
        } else {
            action_log('交易申诉待受理已受理', 'log_admin');
            $this->_jsonp('1000', '操作失败', null);
        }
    }

    public function complain_refund()
    {
        $where['order'] = trim(input('get.id'));
        $data['static'] = 2;
        $data1['static'] = 4;
        if (isset($where) || !empty($where)) {
            $zgh_complain = new Data('orders');
            $zgh_complain->_update($data, $where);

            $zgh_complain = new Data('complain');
            $zgh_complain->_update($data1, $where);

            action_log('修改退款状态成功', 'log_admin', 1);
            $this->_jsonp('200', '操作成功', null);
        } else {
            action_log('修改退款状态失败', 'log_admin');
            $this->_jsonp('1000', '操作失败', null);
        }
    }


    /**
     * 交易申诉详情
     */
    public function complain_msg()
    {
        $where['a.id'] = trim(input('get.id'));

        $join[0] = "__USER__ u";
        $join[1] = "u.uid = a.uid";
        $field = "a.pic,a.problem,a.nick,a.phone,a.time,a.result,a.static,u.username";

        $res = db('complain')->alias('a')->join($join[0], $join[1])->field($field)->where($where)->find();
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }

    }


    /**
     *
     * 交易反馈
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function complain_feedback()
    {
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;

        $username = (isset($_GET['username']) && !empty($_GET['username'])) ? $_GET['username'] : null;
        $logmin = (isset($_GET['logmin']) && $_GET['logmin']) ? strtotime($_GET['logmin']) : 0;
        $logmax = (isset($_GET['logmax']) && $_GET['logmax']) ? (strtotime($_GET['logmax']) + 24 * 60 * 60 - 1) : time();

        $where['a.category_id'] = (isset($_GET['category_id']) && !empty($_GET['category_id'])) ? $_GET['category_id'] : array('neq', 0);
        $where['a.static'] = (isset($_GET['static']) && !empty($_GET['static'])) ? $_GET['static'] : array('neq', 0);
        $where['a.time'] = array(array('egt', $logmin), array('elt', $logmax), 'and');
        $where['u.username|a.phone'] = array('like', "%$username%");

        $join[0] = '__CATEGORY_PROBLEM__ c';
        $join[1] = 'c.id = a.category_id';
		$join1[0] = '__USER__ u';
        $join1[1] = 'u.uid = a.username';
		$filed = "a.id,a.image,a.static,a.phone,a.problem,a.time,u.username,c.name";
		$data = db('feedback')->alias('a')->field($filed)->join($join[0],$join[1])->join($join1[0],$join1[1])->where($where)->page($page)->limit(10)->select();
        //
		$match = new Preg();
		foreach($data as $key=>$val){
                $html=$match->replace($val['problem']);
				$data[$key]['problem']=$html;
            }
			$res['data']=$data;
		$res['count'] = db('feedback')->alias('a')->field($filed)->join($join[0],$join[1])->join($join1[0],$join1[1])->where($where)->count();
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '获取意见反馈信息成功', $res);
        } else {
            $this->_jsonp('1000', '获取意见反馈信息失败', null);
        }
    }


    /**
     * 交易反馈
     * 详情页
     */
    public function complain_feedback_m()
    {
        $id = trim(input('get.id'));
        $where['a.id'] = $id;
		$join[0] = '__CATEGORY_PROBLEM__ c';
        $join[1] = 'c.id = a.category_id';
		$join1[0] = '__USER__ u';
        $join1[1] = 'u.uid = a.username';
        $alias = 'a';
		$filed = "a.id,a.image,a.static,a.phone,a.problem,a.result,a.time,u.username,c.name";
		$data= db('feedback')->alias('a')->field($filed)->join($join[0],$join[1])->join($join1[0],$join1[1])->where($where)->find();
		$match = new Preg();
		foreach($data as $key=>$val){
                $html=$match->replace($val['problem']);
				$data[$key]['problem']=$html;
            }
			$res=$data;
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }

    /**
     * 交易反馈
     * 修改状态
     */
    public function replay_msg()
    {
        $where['id'] = trim(input('get.id'));
        $data['static'] = 2;
        $data['result'] = (isset($_GET['result']) && $_GET['result']) ? trim($_GET['result']) : '';
        if (isset($where) || !empty($where)) {
            $zgh_feedback = new Data('feedback');
            $res = $zgh_feedback->_update($data, $where);
            action_log('交易反馈修改状态', 'log_admin', 1);
            $this->_jsonp('200', '更新成功', $res);
        } else {
            action_log('交易反馈修改状态', 'log_admin');
            $this->_jsonp('1000', '失败', null);
        }
    }

    /**
     * 交易反馈
     * 分类
     */
    public function get_category()
    {
        $res = db('category_problem')->select();
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '成功', $res);

        } else {
            $this->_jsonp('1000', '失败', null);
        }
    }

	public function feedback(){
		$news_list = new news_list();
        $files = request()->file('upfile');

		if (!empty($_FILES['upfile']['name'])) {
			$img = $news_list->upload($files, 'Images/appeal');
			$data['image'] = $img;
		}
		
        $data['problem'] = trim(input('post.news_title'));
        $data['phone'] = trim(input('post.phone'));
		$data['username'] = trim(input('post.username'));
		$data['time'] = time();
        $data['category_id'] = trim(input('post.category_id'));
		$validate = validate('Shop');

		//进行数据验证
		if (!$validate->scene('problem')->check($data)) {
			$this->json('1000',$validate->getError(),null);
		}
		$res = db('feedback')->insert($data);
		if ($res == true) {
            $this->json('200', '反馈成功', null);

        } else {
            $this->json('1000', '反馈失败', null);
        }
	}


}
